package com.spstudio.iforward.end.action;

import java.security.Principal;
import java.util.List;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.enterprise.inject.Model;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.inject.Inject;

import com.spstudio.iforward.end.interfaces.ICandidateStorySessionBeanRemote;
import com.spstudio.iforward.end.interfaces.ISelectionFetcherBeanRemote;
import com.spstudio.iforward.end.interfaces.IStatisticsBeanRemote;
import com.spstudio.iforward.end.qualifier.CandidateStorySessionBeanRemoteQualifier;
import com.spstudio.iforward.end.qualifier.SelectionFetcherRemoteQualifier;
import com.spstudio.iforward.end.qualifier.StatisticsStatelessBeanRemoteQualifier;
import com.spstudio.iforward.front.CandidateStory;

@Model
public class CandidateStoryEditAction {
	
	@Inject @SelectionFetcherRemoteQualifier ISelectionFetcherBeanRemote selectionFetcherBeanRemote;
	@Inject @CandidateStorySessionBeanRemoteQualifier ICandidateStorySessionBeanRemote candidateStorySessionBeanRemote;
	@Inject @StatisticsStatelessBeanRemoteQualifier IStatisticsBeanRemote statisticsBeanRemote;
	@Inject CandidateStory story;
	@Inject Principal principal;
	
	private List<String[]> contributions;
	private List<String[]> categoryCount;
	
	private Logger log = Logger.getLogger("CandidateStoryEditAction");

	@PostConstruct
	public void construct(){
		String id = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
		if (id != null && id.length() != 0){
			try{
				loadStory(Integer.valueOf(id));
			}catch(Exception e){}
			
		}
		contributions = statisticsBeanRemote.retrieveContributionByUser();
		categoryCount = statisticsBeanRemote.retrieveCategoryCount();
	}
	
	public void loadStory(int storyId){
		CandidateStory singleStory = candidateStorySessionBeanRemote.loadSingleCandidateStory(storyId);
		story.setId(singleStory.getId());
		story.setCategoryId(singleStory.getCategoryId());
		story.setTitle(singleStory.getTitle());
		story.setContent(singleStory.getContent());
		story.setComment(singleStory.getComment());
		story.setItemSource(singleStory.getItemSource());
	}
	
	public List<SelectItem> getCategorySelectionList() {
		return selectionFetcherBeanRemote.retrieveStoryCategorySelectionList();
	}

	public String saveCandidateStory(){
		int storyCandidateId = candidateStorySessionBeanRemote.saveCandidateStory(story.clone());
		if (storyCandidateId != -1){
			loadStory(storyCandidateId);
			FacesContext fc = FacesContext.getCurrentInstance();
			fc.addMessage(FacesMessage.FACES_MESSAGES, new FacesMessage(FacesMessage.SEVERITY_INFO, "条目保存成功", ""));
		}
		log.info(principal.getName() + " saved story, id = " + storyCandidateId);
		return null;
	}

	public CandidateStory getStory() {
		return story;
	}

	public void setStory(CandidateStory story) {
		this.story = story;
	}

	public List<String[]> getContributions() {
		return contributions;
	}

	public void setContributions(List<String[]> contributions) {
		this.contributions = contributions;
	}

	public List<String[]> getCategoryCount() {
		return categoryCount;
	}

	public void setCategoryCount(List<String[]> categoryCount) {
		this.categoryCount = categoryCount;
	}

}
